package com.example.softwareconfigurationcoursedesign.LoginRegister;

import com.example.softwareconfigurationcoursedesign.Admin.MD5Encryption;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
// 使用@WebServlet注解定义RegisterServlet类，映射到"/RegisterServlet"路径
@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet{
    // 数据库连接信息
    static final String JDBC_URL = "jdbc:mysql://localhost:3306/course_design";
    static final String DB_USER = "root";
    static final String DB_PASSWORD = "123456";



    // 处理GET请求
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        System.err.println("login do get"); // 输出日志信息
    }

    // 处理POST请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8"); // 设置响应内容类型和字符编码
        request.setCharacterEncoding("utf-8"); // 设置请求字符编码

        // 获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userPhoneNum = request.getParameter("phoneNumber");
        String R_code = request.getParameter("generatedCode2");
        String code = request.getParameter("VerificationCode1");

        // 对密码进行MD5加密
        String encryptedPassword = MD5Encryption.encryptStringToMD5(password);

        System.err.println("register"); // 输出日志信息
        System.err.println(userPhoneNum);
        System.err.println(username);
        System.err.println(password);
        System.err.println(R_code);
        System.err.println(code);

        if (code.equals(R_code)) { // 比较验证码是否正确
            // 连接数据库并插入用户信息
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection connection = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASSWORD);
                String insertQuery = "INSERT INTO users(phone_number,name,password) VALUES (?,?,?)";
                PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
                preparedStatement.setString(1, userPhoneNum);
                preparedStatement.setString(2, username);
                preparedStatement.setString(3, encryptedPassword);
                preparedStatement.executeUpdate();
                preparedStatement.close();
                connection.close();

                PrintWriter out = response.getWriter();
                // 输出注册成功提示并重定向到主页面
                out.println("<script> alert('注册成功 将返回主页面 请您进行登录'); window.location.href='LoginRegisterPage.jsp';</script>");
                out.flush();
                System.out.println("注册用户"); // 输出注册成功信息
            } catch (SQLException e) {
                e.printStackTrace(System.err);
                // 处理数据库错误，重定向到错误页面
                response.sendRedirect("error.html");
            } catch (ClassNotFoundException e) {
                System.err.println("dabsleiufble");
                e.printStackTrace(System.err);
            }
        } else {
            PrintWriter out = response.getWriter();
            // 输出验证码错误提示并重定向到主页面
            out.println("<script> alert('注册失败 验证码输入错误 请您确认后再次输入'); window.location.href='LoginRegisterPage.jsp';</script>");
            out.flush();
        }
    }
}